﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mangox.Common
{
    public class EFHelper
    {


        /// <summary>
        /// EF执行sql语句
        /// 增删改
        /// 泛型约束DbContext并可以实例化
        /// </summary>
        /// <typeparam name="T">DBContext</typeparam>
        /// <param name="sql">sql</param>
        /// <returns></returns>
        public static int ExecuteSqlCommand<T>(string sql) where T : System.Data.Entity.DbContext, new()
        {
            //log.InfoFormat("执行sql{0}" + sql);
            try
            {
                if (string.IsNullOrEmpty(sql))
                {
                    return 0;
                }

                using (T db = new T())
                {
                    return db.Database.ExecuteSqlCommand(sql);
                }
            }
            catch (Exception ex)
            {
                throw ex;
                //log.InfoFormat("执行失败：sql-{0}，Fail-{1}", ex);
            }
        }

    }
}
